import tkinter as tk
from PIL import Image, ImageTk
from tkinter import ttk,messagebox

import Statistics

# 示例问题数据
questions = [
    {
        "question": "What is the capital of France?",
        "options": ["Berlin", "Madrid", "Paris", "Rome"],
        "answer": "Paris"
    },
    {
        "question": "Which planet is known as the Red Planet?",
        "options": ["Earth", "Mars", "Jupiter", "Saturn"],
        "answer": "Mars"
    },
    {
        "question": "Who wrote 'To Kill a Mockingbird'?",
        "options": ["Harper Lee", "J.K. Rowling", "Ernest Hemingway", "Mark Twain"],
        "answer": "Harper Lee"
    }
]

class ViewQuestionnaire(tk.Toplevel):
    def __init__(self, parent,data):
        super().__init__()

        # 数据初始化
        self.data=data

        self.parent = parent
        self.title("问卷详情")

        # 获取屏幕的宽和高
        sreen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()

        # 设置窗口的宽和高
        root_width = int(sreen_width / 2)
        root_height = int(screen_height / 2)

        # 使窗口居中
        self.geometry("{}x{}+{}+{}".format(
            root_width, root_height,
            int((sreen_width - root_width) / 2), int((screen_height - root_height) / 2)
        ))



        # 使用 Pillow 加载图像
        image = Image.open("Image/return.png")
        # 调整图像大小
        new_size = (25, 25)  # 设置新的尺寸 (宽度, 高度)
        resized_image = image.resize(new_size, Image.LANCZOS)
        photo = ImageTk.PhotoImage(resized_image)

        self.back_button = tk.Button(self, image=photo, text='返回', font=("宋体", 15), compound='left',
                                     command=self.back_to_main)
        self.back_button.photo = photo  # 保存图像对象的引用
        self.back_button.place(relx=0.01, rely=0.01, relwidth=0.15, relheight=0.1)

        # TODO 创建文本区域 插入数据
        # 创建标题区域
        self.tit_area1 = tk.Text(self, wrap=tk.WORD, state=tk.DISABLED)
        self.tit_area1.place(relx=0.4, rely=0.05, relwidth=0.2, relheight=0.1)

        # # 创建文本区域
        # self.text_area = tk.Text(self, wrap=tk.WORD, state=tk.DISABLED)
        # self.text_area.place(relx=0.01,rely=0.15,relwidth=0.98,relheight=0.6)

        # 创建统计报表键
        self.statistics_button = tk.Button(self,text='统计报表',font=("宋体", 15), compound='center',command=self.display)
        self.statistics_button.place(relx=0.2,rely=0.8,relwidth=0.15,relheight=0.1)

        # 创建图形显示键
        self.statistics_button = tk.Button(self,text='图形显示',font=("宋体", 15), compound='center',command=self.statistics_key_function)
        self.statistics_button.place(relx=0.45,rely=0.8,relwidth=0.15,relheight=0.1)

        # 创建Canvas和Scrollbar
        canvas = tk.Canvas(self)
        scrollbar = tk.Scrollbar(self, orient="vertical", command=canvas.yview)
        scrollable_frame = tk.Frame(canvas)

        scrollable_frame.bind(
            "<Configure>",
            lambda e: canvas.configure(
                scrollregion=canvas.bbox("all")
            )
        )

        canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
        canvas.configure(yscrollcommand=scrollbar.set)

        canvas.place(relx=0.01, rely=0.2, relwidth=0.97, relheight=0.5)
        scrollbar.place(relx=0.96, rely=0.2, relwidth=0.03, relheight=0.5)

        selected_options = {}  # 存储每个问题的selected_option变量

        for i, q in enumerate(questions):
            question_label = tk.Label(scrollable_frame, text=f"Q{i + 1}: {q['question']}")
            question_label.pack(anchor="w", padx=10, pady=5)

            # 创建独立的变量名称
            var_name = f"selected_option{i + 1}"
            selected_options[var_name] = tk.StringVar(value=q["answer"])  # 自动选中正确答案

            for option in q["options"]:
                radio_button = tk.Radiobutton(scrollable_frame, text=option, variable=selected_options[var_name],
                                              value=option)
                radio_button.pack(anchor="w", padx=20)

            # answer_label = tk.Label(scrollable_frame, text=f"Answer: {q['answer']}", fg="green")
            # answer_label.pack(anchor="w", padx=10, pady=5)
            #
            # # 打印设置的选项值，确认选中正确答案
            # print(f"Selected answer for Q{i + 1}: {selected_options[var_name].get()}")  # 获取实际值

        # 返回键功能

    def back_to_main(self):
        self.destroy()
        self.parent.deiconify()

    # 统计报表功能
    def display(self):
        self.withdraw()
        display = Display.Display(self)
        display.mainloop()


        # 统计键功能

    def statistics_key_function(self):
        self.withdraw()
        statistics = Statistics.Statistics(self)
        statistics.mainloop()

        # 导出键功能集合

    def export_key_function(self):
        self.on_submit()

        # 导出键功能-消息窗口弹出

    def on_submit(self):
        messagebox.showinfo('Info', '导出完成')